using System;
using System.IO;
using System.Collections.Generic;
using System.Text;

namespace MyMedia.iPod.Music
{
	public class TrackItem : Node
	{
		string m_title;
		string m_location;
		string m_album;
		string m_artist;
		string m_genre;
        int m_Time;
		int m_fileSize;
		int m_trackNumber;
		int m_totalTracks;

		internal Mhit m_mhit;

		public static TrackItem Create(string title, string album, string artist)
		{
			TrackItem item = new TrackItem();
			item.m_mhit = Mhit.Create();
			item.m_mhit.StringCount = 3;
			item.AddChild(DataString.CreateString(title, 1));
			item.AddChild(DataString.CreateString(album, 3));
			item.AddChild(DataString.CreateString(artist, 4));

			return item;
		}

		protected override int Size
		{
			get { return 0; /* m_mhit.HeaderLength;*/ }
		}

		protected override void ReadImpl(BinaryReader reader)
		{
			m_mhit = new Mhit();
			m_mhit.Read(reader);

			m_fileSize = m_mhit.Size;
			m_trackNumber = m_mhit.TrackNumber;
			m_totalTracks = m_mhit.TotalTracks;
            m_Time = m_mhit.Length;

			for (int j = 0; j < m_mhit.StringCount; j++)
			{
				DataObject dataObj = new DataObject();
				AddChild(dataObj);
				dataObj.Read(reader);
				
				if (dataObj.Type < 15)
				{
					DataString dataString = new DataString();
					dataObj.AddChild(dataString);
					dataString.Read(reader);

					if (dataObj.Type == 1)
						m_title = dataString.Value;
					else if (dataObj.Type == 2)
						m_location = ConvertToFilePath(dataString.Value);
					else if (dataObj.Type == 3)
						m_album = dataString.Value;
					else if (dataObj.Type == 4)
						m_artist = dataString.Value;
					else if (dataObj.Type == 5)
						m_genre = dataString.Value;
				}
				else
				{
					DataBlob dataBlob = new DataBlob();
					dataObj.AddChild(dataBlob);
					dataBlob.Read(reader);
				}
			}
		}

		protected override void WriteImpl(BinaryWriter writer)
		{
			m_mhit.Write(writer);
		}

		protected override void SetSize(int size)
		{
			m_mhit.TotalLength = size;
		}

		public string Title
		{
			get { return m_title; }
		}

		public string Location
		{
			get { return m_location; }
		}

		public string Album
		{
			get { return m_album; }
		}

		public string Artist
		{
			get { return m_artist; }
		}

		public string Genre
		{
			get { return m_genre; }
		}

		public int FileSize
		{
			get { return m_fileSize; }
		}

		public int TrackNumber
		{
			get { return m_trackNumber; }
		}

		public int TotalTracks
		{
			get { return m_totalTracks; }
		}

        public int Length
        {
            get { return m_Time; }
        }

		private string ConvertToFilePath(string location)
		{
			string filePath = string.Empty;
			if (string.IsNullOrEmpty(location))
				return location;
			if (location.Length <= 0)
				return location;

			if (location[0] == ':')
				filePath = location.Remove(0, 1);
			
			filePath = filePath.Replace(':', '\\');

			return filePath;
		}
	}
}
